package belf.migrate.evaluate.helper;

import belf.migrate.api.taskconf.EvaluateConf;
import belf.migrate.api.taskconf.JobContext;
import belf.migrate.evaluate.model.EvaluateObjectsResult;
import belf.migrate.evaluate.model.EvaluateWholeResult;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/**
 * 整体迁移风险评估
 */
@Slf4j
public class EvaluateWholeTool {

    protected JobContext jobContext;

    protected EvaluateObjectsResult objectsResult;

    private int totalScore;


    public EvaluateWholeTool(JobContext jobContext, EvaluateObjectsResult objectsResult, int totalScore) {
        this.jobContext = jobContext;
        this.objectsResult = objectsResult;
        this.totalScore = totalScore;
    }

    public EvaluateWholeResult run() {

        EvaluateConf evaluateConf = jobContext.getTaskConf().getEvaluateConf();
        EvaluateWholeResult evaluateWholeResult = new EvaluateWholeResult();

        evaluateWholeResult.setNumberViews( objectsResult.getViewResult()== null ? null :objectsResult.getViewResult().getAllObjectSize());
        evaluateWholeResult.setNumberTriggers(objectsResult.getTriggerResult() == null ? null : objectsResult.getTriggerResult().getAllObjectSize());
        evaluateWholeResult.setNumberProcedures(objectsResult.getProcedureResult() == null ? null : objectsResult.getProcedureResult().getAllObjectSize());
        evaluateWholeResult.setNumberSequences(objectsResult.getSeqResult() == null ? null : objectsResult.getSeqResult().getAllObjectSize());
        evaluateWholeResult.setNumberSynonyms(objectsResult.getSynonymResult() == null ? null : objectsResult.getSynonymResult().getAllObjectSize());
        evaluateWholeResult.setObjectSize(objectsResult.getAllObjectSize());
        evaluateWholeResult.setDataSize(objectsResult.getTableResult()== null ? null : objectsResult.getTableResult().getAllTableSize());
        evaluateWholeResult.setAllTableRows(objectsResult.getTableResult().getAllTableRows());

        Map<String, Double> scoreMap = new HashMap<>();
        for (String sinkCatalog : jobContext.getTaskConf().getEvaluateConf().getSinkDbs()) {
            scoreMap.put(sinkCatalog, (double) totalScore / 100);
        }
        evaluateWholeResult.setCompatiblePercent(scoreMap);
        return evaluateWholeResult;
    }
}
